package com.store.daoimpl;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.store.dao.UserDao;
import com.store.pojo.User;
import com.stroe.util.HibernateSessionFactory;

public class UserDaoImpl extends GeneralDaoImpl<User> implements UserDao {

	@Override
	public boolean login(String username, String password) {
		Session session = HibernateSessionFactory.getSession();
		try {
			Criteria criteria = session.createCriteria(User.class);
			criteria.add(Restrictions.eq("ULoginname", username));
			criteria.add(Restrictions.eq("UPassword", password));
			@SuppressWarnings("unchecked")
			List<User> list = criteria.list();
			Iterator<User> iterator = list.iterator();
			if (iterator.hasNext()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return false;
	}

	@Override
	public boolean isNewUserName(String username) {
		Session session = HibernateSessionFactory.getSession();
		try {
			Criteria criteria = session.createCriteria(User.class);
			criteria.add(Restrictions.eq("ULoginname", username));
			@SuppressWarnings("unchecked")
			List<User> list = criteria.list();
			if (list != null && list.size() > 0) {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
}
